package com.mlming.aspect;

import lombok.extern.slf4j.Slf4j;
import org.simpleframework.aop.annotation.Aspect;
import org.simpleframework.aop.annotation.Order;
import org.simpleframework.aop.aspect.DefaultAspect;
import org.simpleframework.core.annotation.Controller;

import java.lang.reflect.Method;

@Slf4j
@Aspect(pointcut = "execution(* com.mlming.controller.superadmin.HeadLineOperationController.*(..))")
@Order(0)
public class ControllerRuntimeRecordAspect extends DefaultAspect {
    private long time;
    @Override
    public void before(Class<?> targetClass, Method method, Object[] args) throws Throwable {
        log.info("开始计时，执行的类是[{}], 执行的方法是[{}], 参数是[{}]",targetClass.getName(),method.getName(),args);
        time = System.currentTimeMillis();
    }

    @Override
    public Object afterReturning(Class<?> targetClass, Method method, Object[] args, Object returnValue) throws Throwable {
        long endTime = System.currentTimeMillis();
        long costTime = endTime - time;
        log.info("正确结束计时，执行的类是[{}], 执行的方法是[{}], 参数是[{}]，执行的时间是[{}]",targetClass.getName(),method.getName(),args,costTime);
        return returnValue;
    }

    @Override
    public void afterThrowing(Class<?> targetClass, Method method, Object[] args, Throwable e) throws Throwable {
        long endTime = System.currentTimeMillis();
        long costTime = endTime - time;
        log.info("错误结束计时，执行的类是[{}], 执行的方法是[{}], 参数是[{}]，执行的时间是[{}]",targetClass.getName(),method.getName(),args,costTime);
    }
}
